home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / dtree.h < prev    next >
C/C++ Source or Header  |  1999-03-17  |  4KB  |  126 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- //
  5. // C++ Header File Name: dtree.h 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 02/07/1997  
  9. // Date Last Modified: 03/17/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ---------- Include File Description and Details  ---------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. Disk-based binary search tree used to create file-based objects
  32. that reside on disk.
  33. */
  34. // ----------------------------------------------------------- //   
  35. #ifndef __DTREE_HPP
  36. #define __DTREE_HPP
  37.  
  38. // NOTE: To avoid portablity problems with template classes, enable
  39. // the __NOT_USING_TEMPLATE_CLASS__ macro and directly code the
  40. // Bucket class, Cache class, and the CachePtr class for the data
  41. // type that will be used.
  42.  
  43. // Default to using template class
  44. #ifndef __NOT_USING_TEMPLATE_CLASS__
  45. #define __USING_TEMPLATE_CLASS__
  46. #endif
  47.  
  48. #ifdef __USING_TEMPLATE_CLASS__
  49. #include "tnode.h"
  50. #endif
  51.  
  52. #ifdef __NOT_USING_TEMPLATE_CLASS__
  53. #include "cacheptr.h"
  54. typedef CachePtr CachePointer; // Pointer to cache
  55. #endif
  56.  
  57. // (T)ree (H)eader
  58. struct TreeHeader
  59. {
  60.   FAU RootAddress;
  61. }; 
  62.  
  63. // DTree class (Disk-based binary search Tree)
  64. class DTree
  65. {
  66. public:
  67.   DTree(int cache_sz = 8) : FilePtr(0), cache(cache_sz), Root(cache) { }
  68.   ~DTree() { Disconnect(); }
  69.  
  70. public:
  71.   int Open(char *FName, VBDFile::AccessMode mode,
  72.        FAU FileAddress=sizeof(FileHeader));
  73.   
  74.   int Connect(VBDFilePtr &fp, int create,
  75.           FAU FileAddress = sizeof(FileHeader));
  76.   
  77.   void Disconnect();
  78.   int Create(char *FName, FAU FileAddress = sizeof(FileHeader));
  79.   void Flush();
  80.   void Close() { Disconnect(); }
  81.   CachePointer GetMember(const DTYPE &X);
  82.   CachePointer Add(const DTYPE &X, int &existed);
  83.   CachePointer Change(const DTYPE &A, const DTYPE &B);
  84.   CachePointer Search(const DTYPE &X);
  85.   CachePointer Detach(const DTYPE &X);
  86.   int Delete(const DTYPE &X);
  87.   CachePointer GetRoot() { return Root; }
  88.  
  89. #ifdef __USING_TEMPLATE_CLASS__
  90.   Cache<TNode> GetCache() { return cache; }
  91. #endif
  92.  
  93. #ifdef __NOT_USING_TEMPLATE_CLASS__
  94.   Cache GetCache() { return cache; }
  95. #endif
  96.  
  97. protected:
  98.   void WriteHdr();
  99.   void ReadHdr();
  100.   CachePointer ParentOfSuccessor(CachePointer Tree);
  101.   CachePointer SearchP(const DTYPE &X, CachePointer &p, int &side);
  102.   CachePointer DetachNode(CachePointer Tree, CachePointer p, int side);
  103.  
  104. protected:
  105.   VBDFilePtr FilePtr; // File manager for the tree
  106.   TreeHeader THeader; // Tree header
  107.   FAU THeaderAddress; // Where the tree header is stored
  108.  
  109. public:
  110.   CachePointer Root;
  111.  
  112. #ifdef __USING_TEMPLATE_CLASS__
  113.   Cache<TNode> cache;
  114. #endif
  115.  
  116. #ifdef __NOT_USING_TEMPLATE_CLASS__
  117.   Cache cache;
  118. #endif
  119. };
  120.  
  121. #endif  // __DTREE_HPP //
  122. // ----------------------------------------------------------- // 
  123. // ------------------------------- //
  124. // --------- End of File --------- //
  125. // ------------------------------- //
  126.